package com.jisf.driver.exception;

import com.jisf.driver.response.Result;
import com.jisf.driver.response.ResultCode;
import com.jisf.driver.utils.LogUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Classname GlobalExceptionHandler
 * @Description TODO
 * @Date 2022/4/4 19:48
 * @Created by jisf：【429627912@qq.com】
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    @Autowired
    private LogUtil log;

//    @ExceptionHandler(Exception.class)
//    public Result resolveException(Exception e){
//        e.printStackTrace();
//        return Result.error(e);
//    }

    /**
     * 处理Token异常
     * @param e
     * @return
     */
    @ExceptionHandler(TokenException.class)
    public Result<?> resolveTokenException(TokenException e){
        log.error("Token异常：", e);
        return Result.error(e.getCode(), e.getMessage());
    }

    /**
     * 处理Login异常
     * @param e
     * @return
     */
    @ExceptionHandler(LoginException.class)
    public Result<?> resolveLoginException(LoginException e){
        log.error("登录异常：" ,e);
        return Result.error(e.getCode(), e.getMessage());
    }

    /**
     * 处理DriverSchool异常
     * @param e
     * @return
     */
    @ExceptionHandler(DrivingSchoolException.class)
    public Result<?> resolveDrivingSchoolException(DrivingSchoolException e){
        log.error(e.getMessage() ,e.getE());
        return Result.error(e.getCode(), e.getMessage());
    }

    /**
     * 处理System异常
     * @param e
     * @return
     */
    @ExceptionHandler(SystemException.class)
    public Result<?> resolveSystemException(SystemException e){
        log.error("系统内部异常：" ,e.getE());
        return Result.error(ResultCode.SYSTEM_FAILED);
    }

}
